package com.tianpengtech.front.website;

import org.apache.commons.lang3.StringUtils;

import com.jfinal.core.Controller;
import com.tianpengtech.admin.dictionary.DictionaryDao;
import com.tianpengtech.common.model.dictionary.Dictionary;
import com.tianpengtech.common.model.websitead.WebSiteAd;
import com.tianpengtech.common.model.websiteemployee.WebSiteEmployee;
import com.tianpengtech.common.model.websitemsg.WebSiteMsg;
import com.tianpengtech.common.model.websitewh.WebSiteWh;
import com.tianpengtech.common.search.LikeCondition;
import com.tianpengtech.common.search.QueryConstructor;
import com.tianpengtech.common.util.JsonUtil;

public class WebSiteController extends Controller {

	public void index() {
//		String sql ="select *";
//		setAttr("obj", WebSiteWh.dao.paginate(1,6,sql,"from "+WebSiteWh.dao.tableName()+" order by id desc"));
//		setAttr("lable",DictionaryDao.ME.readParents(Dictionary.WEB_SITE_LABEL));
		setAttr("flag","index");
		String sql="select * from "+WebSiteAd.dao.tableName()+" where category =? order by sort desc";
		setAttr("ads", WebSiteAd.dao.find(sql,WebSiteAd.INDEX_BANNER));
		renderTemplate("index.html");
	}

	public void contactus() {
		setAttr("flag","contactus");
		setAttr("ad",queryBannerByType(WebSiteAd.CONTACTUS_BANNER));
		renderTemplate("contactus.html");
	}
	
	public void doContactUs(){
		WebSiteMsg webSiteMsg = getModel(WebSiteMsg.class);
		webSiteMsg.save();
		renderJson(JsonUtil.printSuccess("留言成功！感谢你的参与"));
	}

	public void aboutus() {
		setAttr("ad",queryBannerByType(WebSiteAd.ABOUTUS_BANNER));
		setAttr("flag","aboutus");
		renderTemplate("aboutus.html");
	}
	
	
	public void business() {
		setAttr("flag","business");
		setAttr("ad",queryBannerByType(WebSiteAd.CORE_BUSINESS_BANNER));
		renderTemplate("business.html");
	}
	
	
	public void fair() {
		int pageNum = getParaToInt("pageNum",1);
		String lable = getPara("lable","");
		String sql ="select *";
		
		QueryConstructor constructor  = new QueryConstructor();
		constructor.setCommand("from "+WebSiteWh.dao.tableName());
		constructor.setOrderSql(" order by sort desc,id desc ");
		if(StringUtils.isNotEmpty(lable)){
			constructor.and(new LikeCondition(WebSiteWh.LABLE, "%"+lable+"%"));
		}
		
		setAttr("obj", WebSiteWh.dao.paginate(pageNum, 6,sql,constructor.buildSQL(),constructor.getParams().toArray()));
		
		setAttr("lable",DictionaryDao.ME.readParents(Dictionary.WEB_SITE_LABEL));
		setAttr("kw",lable);
		setAttr("flag","fair");
		setAttr("pageNum",pageNum);
		setAttr("ad",queryBannerByType(WebSiteAd.FAIR_BANNER));
		renderTemplate("fair.html");
	}
	
	
	public void employee() {
		setAttr("obj", WebSiteWh.dao.find("select * from "+WebSiteEmployee.dao.tableName()+" order by id desc"));
		setAttr("flag","employee");
		setAttr("ad",queryBannerByType(WebSiteAd.HIRE_BANNER));
		renderTemplate("employee.html");
	}
	
	
	
	
	private WebSiteAd queryBannerByType(String type){
		String sql="select * from "+WebSiteAd.dao.tableName()+" where category =? order by sort desc";
		return WebSiteAd.dao.findFirst(sql,type);
	}
	
}
